﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TWS.Controller.Paging;
using System.IO;
using TWS.Client;
using System.Data;

public partial class admin_QuanLyTuyenDung : BaseAdminPage
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {

            //tab them moi
            LoadData();
            LoadTuyenDung(Convert.ToInt32(ddlLoaiBai.SelectedValue), Convert.ToInt32(ddlLang.SelectedValue));
        }
    }

    private void LoadTuyenDung(int loai, int lang)
    {
        string tmpPage = Request.QueryString["pg"];
        int currentPage = 1;
        if (tmpPage != null && tmpPage != "")
        {
            currentPage = Convert.ToInt32(tmpPage);
        }

        TuyenDungDataSet spDs = new TuyenDungHelper().LoadTuyenDung(currentPage, PagingSize, "NgayDang", "desc", loai, lang);

        repSanPham.DataSource = spDs.TuyenDung;
        repSanPham.DataBind();

        TotalRecord = new TuyenDungHelper().LoadTuyenDung(loai, lang).TuyenDung.Rows.Count;

        ResultCount.Text = TotalRecord.ToString();

        Paging page = getPagingObject(TotalRecord, currentPage, PagingSize);

        PageString = page.BuildPaging();
        LoadHinhMacDinh();
        txtLink.Text = TrangTinhHelper.GetImageLogo("LinkIdClient") == "logo.jpg" ? "" : TrangTinhHelper.GetImageLogo("LinkIdClient");
    }

    private void LoadHinhMacDinh()
    {

        TextBox1.Text = TrangTinhHelper.GetImageLogo("TuyenDung" + ddlLoaiBai.SelectedValue);
        string saveLocation = MyFilePathHelper.GetImageFolderURL(this);
        this.imageDemons.Src = saveLocation + TextBox1.Text;

    }

    private void LoadData()
    {
        ddlLoaiBai.DataSource = LoaiDataHelper.LoadLoaiTuyenDung();
        ddlLoaiBai.DataTextField = "Value";
        ddlLoaiBai.DataValueField = "Key";
        ddlLoaiBai.DataBind();

        ddlLang.DataSource = LoaiDataHelper.LoadNgonNgu();
        ddlLang.DataTextField = "Value";
        ddlLang.DataValueField = "Key";
        ddlLang.DataBind();

        ddlLang1.DataSource = LoaiDataHelper.LoadNgonNgu();
        ddlLang1.DataTextField = "Value";
        ddlLang1.DataValueField = "Key";
        ddlLang1.DataBind();

        ddlLoaiBai.SelectedValue = Request.QueryString["id"];
        Checkvisible();
    }

    private void Checkvisible()
    {
        noidung.Visible = true;
            link.Visible = true;
        if (ddlLoaiBai.SelectedValue=="1")
        {
            noidung.Visible = false;
        }
        else
        {
            link.Visible = false;
        }
    }

    protected void ddlLoaiBai1_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        
    }

    protected void ddlLoaiBai_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        int loai = Convert.ToInt32(ddlLoaiBai.SelectedValue);
        int lang = Convert.ToInt32(ddlLang.SelectedValue);

            LoadTuyenDung(loai, lang);
        
        Checkvisible();
    }

    protected void ddlLang_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        int loai = Convert.ToInt32(ddlLoaiBai.SelectedValue);
        int lang = Convert.ToInt32(ddlLang.SelectedValue);
        LoadTuyenDung(loai, lang);
    }

    //public string GetLoaiSanPham(object loaisanphamID)
    //{
    //    if (loaisanphamID != DBNull.Value)
    //    {
    //        LoaiSanPhamDataSet lspDS = new LoaiSanPhamHelper().LoadLoaiSanPham(Convert.ToInt32(loaisanphamID));
    //        return lspDS.LoaiSanPham[0].TenLoaiSanPham;

    //    }
    //    return "";
    //}

    protected void btnOK_OnClick(object sender, EventArgs e)
    {
        Page.Validate();
        if (!Page.IsValid) return;
        TuyenDungHelper spHP = new TuyenDungHelper();
        TuyenDungDataSet spDS = new TuyenDungDataSet();
        TuyenDungDataSet.TuyenDungRow TuyenDungmoi = spDS.TuyenDung.NewTuyenDungRow();
        TuyenDungmoi.Ten = txtTenSanPham.Text;
        if (txtLink.Text != "") TuyenDungmoi.Link = txtLink.Text;
        TuyenDungmoi.Loai = Convert.ToInt32(2);
        TuyenDungmoi.Lang = Convert.ToInt32(ddlLang1.SelectedValue);
        TuyenDungmoi.Link = txtLink.Text;
        TuyenDungmoi.NoiDung = txtNoiDung.Value;
        TuyenDungmoi.NgayDang = DateTime.Now;
        spDS.TuyenDung.AddTuyenDungRow(TuyenDungmoi);
        spHP.SaveTuyenDung(spDS);
        RedirectPage(Request.RawUrl, true);
    }


    protected void learnMoreButton_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(((Button)sender).CommandArgument);
        TuyenDungDataSet bannerDs = new TuyenDungHelper().LoadTuyenDung(id);
        bannerDs.TuyenDung[0].Delete();
        new TuyenDungHelper().SaveTuyenDung(bannerDs);
        RedirectPage(Request.RawUrl, true);
    }

    protected void repSanPham_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Button repCon = ((Button)e.Item.FindControl("learnMoreButton"));
            repCon.Attributes.Add("onclick", "if (!confirm('Xóa dữ liệu?')) return;");
        }
    }

    //public string GetLoaiSanPhamName()
    //{
    //    LoaiSanPhamHelper lspHP = new LoaiSanPhamHelper();
    //    LoaiSanPhamDataSet lspDs = lspHP.LoadLoaiSanPham(Convert.ToInt32(ddlCon.SelectedValue));
    //    if (lspDs.LoaiSanPham.Rows.Count == 0)
    //    {
    //        return "Tất cả";
    //    }
    //    LoaiSanPhamDataSet.LoaiSanPhamRow lsp = lspDs.LoaiSanPham[0];
    //    if (lsp.IsLoaiSanPhamChaIDNull())
    //    {
    //        return lsp.TenLoaiSanPham;
    //    }
    //    LoaiSanPhamDataSet lspDsCha = lspHP.LoadLoaiSanPham(lsp.LoaiSanPhamChaID);
    //    LoaiSanPhamDataSet.LoaiSanPhamRow lspCha = lspDsCha.LoaiSanPham[0];

    //    return lspCha.TenLoaiSanPham + "  >>  " + lsp.TenLoaiSanPham;
    //}

    public string GetNgayTao(object ngaytao)
    {
        if (ngaytao != DBNull.Value)
        {
            return CommonHelper.ConvertDate(ngaytao, Constants.AdminUI);

        }
        return "";
    }

    public string GetTenLoai(object id)
    {
       return LoaiDataHelper.LoadLoaiTuyenDung()[id].ToString();
    }

    protected void btnUpload_OnClick(object sender, EventArgs e)
    {
        try
        {


            UploadImageLogo();
            if (TextBox1.Text == string.Empty)
            {
                TextBox1.Text = "logo.jpg";
            }
            TrangTinhHelper tthelper = new TrangTinhHelper();
            TrangTinhDataSet ttDs = tthelper.LoadTrangTinhViTri("TuyenDung" + ddlLoaiBai.SelectedValue, 1);
            if (ttDs.TrangTinh.Rows.Count > 0)
            {
                ttDs.TrangTinh[0].NoiDung = TextBox1.Text;
                tthelper.SaveTrangTinh(ttDs);
            }
        }
        catch (Exception ex)
        {
            //DisplayMesseage(ex.Message);
            this.RedirectPage("pgError.aspx", false);
        }
        RedirectPage(Request.RawUrl, true);
    }

    private void UploadImageLogo()
    {
        if (TextBox1.Text != string.Empty)
        {
            //delete old image
            try
            {
                string saveLocation = MyFilePathHelper.GetImageFolderURL_Saving(this);
                MyFilePathHelper.DeleteResource(saveLocation, TextBox1.Text);
            }
            catch (Exception)
            {
            }
            finally
            {
                TextBox1.Text = "noimage.jpg";
            }
            //postpone new image
        }
        //save new image
        if (this.File1.PostedFile != null && this.File1.PostedFile.ContentLength > 0)
        {
            string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
            string path = MyFilePathHelper.GetImageFolderURL_Saving(this);
            fn = MyFilePathHelper.AppendTimeStamp(fn);
            string saveLocation = path + fn;

            File1.PostedFile.SaveAs(saveLocation);
            FileStream fs = File.Open(saveLocation, FileMode.Open, FileAccess.Read);
            byte[] inByte = new byte[fs.Length];
            fs.Read(inByte, 0, inByte.Length);
            fs.Close();
            MyFilePathHelper.DeleteResource(path, fn);
            ImagesConvertHelper iconvert = new ImagesConvertHelper();
            string filename;
            if (iconvert.ConvertImage(saveLocation, inByte, 6, out filename) == false)
                TextBox1.Text = string.Empty;
            else
            {
                TextBox1.Text = fn + ".JPG";
            }
        }
    }

    protected void btnSAveClient_OnClick(object sender, EventArgs e)
    {
        try
        {
            TrangTinhHelper tthelper = new TrangTinhHelper();
            TrangTinhDataSet ttDs = tthelper.LoadTrangTinhViTri("LinkIdClient", 1);
            if (ttDs.TrangTinh.Rows.Count > 0)
            {
                ttDs.TrangTinh[0].NoiDung = txtLink.Text;
                tthelper.SaveTrangTinh(ttDs);
            }
        }
        catch (Exception ex)
        {
            //DisplayMesseage(ex.Message);
            this.RedirectPage("pgError.aspx", false);
        }
        RedirectPage(Request.RawUrl, true);
    }

}